package com.cmiov.pulsar.pulsardemo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.pulsar.functions.api.SerDe;

import java.io.IOException;

/**
 * * Licensed to CMDT,Inc. under the terms of the CMDT
 * Software License version 1.0.
 * <p>
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 * ----------------------------------------------------------------------------
 * Date           Author               Version        Comments
 * 18-12-26       joe chow                 1.0            Initial Version
 */
public class UserSerDe implements SerDe<User> {

    private ObjectMapper mapper = new ObjectMapper();

    @Override
    public User deserialize(byte[] input) {
        String json = new String(input);
        try {
            return mapper.readValue(input, User.class);
        } catch (IOException e) {
            return null;
        }
    }

    @Override
    public byte[] serialize(User input) {
        try {
            return mapper.writeValueAsBytes(input);
        } catch (JsonProcessingException e) {
            return null;
        }
    }
}
